﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ERPBackend.Migrations
{
    /// <inheritdoc />
    public partial class AddProductionStockInTables : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "purchase_stock_in",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    stock_in_code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    purchase_order_id = table.Column<int>(type: "int", nullable: false),
                    supplier_id = table.Column<int>(type: "int", nullable: false),
                    warehouse_id = table.Column<int>(type: "int", nullable: false),
                    stock_in_date = table.Column<DateTime>(type: "datetime2", nullable: false),
                    total_quantity = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    total_amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    approved_by = table.Column<int>(type: "int", nullable: true),
                    approved_time = table.Column<DateTime>(type: "datetime2", nullable: true),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_purchase_stock_in", x => x.id);
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_purchase_order_purchase_order_id",
                        column: x => x.purchase_order_id,
                        principalTable: "purchase_order",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_supplier_supplier_id",
                        column: x => x.supplier_id,
                        principalTable: "supplier",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_warehouse_warehouse_id",
                        column: x => x.warehouse_id,
                        principalTable: "warehouse",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "workshop",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    workshop_code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    workshop_name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    department_id = table.Column<int>(type: "int", nullable: false),
                    manager_id = table.Column<int>(type: "int", nullable: true),
                    phone = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    address = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: true),
                    description = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    status = table.Column<byte>(type: "tinyint", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_workshop", x => x.id);
                    table.ForeignKey(
                        name: "FK_workshop_department_department_id",
                        column: x => x.department_id,
                        principalTable: "department",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_workshop_user_manager_id",
                        column: x => x.manager_id,
                        principalTable: "user",
                        principalColumn: "id");
                });

            migrationBuilder.CreateTable(
                name: "purchase_stock_in_item",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    purchase_stock_in_id = table.Column<int>(type: "int", nullable: false),
                    purchase_order_item_id = table.Column<int>(type: "int", nullable: true),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    material_name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    specification = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    unit = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    order_quantity = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    stock_in_quantity = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    unit_price = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    amount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    batch_no = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    production_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    expiry_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_purchase_stock_in_item", x => x.id);
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_item_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_item_purchase_order_item_purchase_order_item_id",
                        column: x => x.purchase_order_item_id,
                        principalTable: "purchase_order_item",
                        principalColumn: "id");
                    table.ForeignKey(
                        name: "FK_purchase_stock_in_item_purchase_stock_in_purchase_stock_in_id",
                        column: x => x.purchase_stock_in_id,
                        principalTable: "purchase_stock_in",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "production_stock_in",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    stock_in_code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    production_order_id = table.Column<int>(type: "int", nullable: false),
                    workshop_id = table.Column<int>(type: "int", nullable: false),
                    warehouse_id = table.Column<int>(type: "int", nullable: false),
                    stock_in_date = table.Column<DateTime>(type: "datetime2", nullable: false),
                    total_quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    status = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    approved_by = table.Column<int>(type: "int", nullable: true),
                    approved_time = table.Column<DateTime>(type: "datetime2", nullable: true),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_production_stock_in", x => x.id);
                    table.ForeignKey(
                        name: "FK_production_stock_in_warehouse_warehouse_id",
                        column: x => x.warehouse_id,
                        principalTable: "warehouse",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_production_stock_in_workshop_workshop_id",
                        column: x => x.workshop_id,
                        principalTable: "workshop",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "production_stock_in_item",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    production_stock_in_id = table.Column<int>(type: "int", nullable: false),
                    production_order_item_id = table.Column<int>(type: "int", nullable: true),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    product_name = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    specification = table.Column<string>(type: "nvarchar(255)", maxLength: 255, nullable: false),
                    unit = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    order_quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    stock_in_quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    batch_no = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    production_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    expiry_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    remark = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_production_stock_in_item", x => x.id);
                    table.ForeignKey(
                        name: "FK_production_stock_in_item_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_production_stock_in_item_production_stock_in_production_stock_in_id",
                        column: x => x.production_stock_in_id,
                        principalTable: "production_stock_in",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_production_stock_in_warehouse_id",
                table: "production_stock_in",
                column: "warehouse_id");

            migrationBuilder.CreateIndex(
                name: "IX_production_stock_in_workshop_id",
                table: "production_stock_in",
                column: "workshop_id");

            migrationBuilder.CreateIndex(
                name: "IX_production_stock_in_item_material_id",
                table: "production_stock_in_item",
                column: "material_id");

            migrationBuilder.CreateIndex(
                name: "IX_production_stock_in_item_production_stock_in_id",
                table: "production_stock_in_item",
                column: "production_stock_in_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_purchase_order_id",
                table: "purchase_stock_in",
                column: "purchase_order_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_supplier_id",
                table: "purchase_stock_in",
                column: "supplier_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_warehouse_id",
                table: "purchase_stock_in",
                column: "warehouse_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_item_material_id",
                table: "purchase_stock_in_item",
                column: "material_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_item_purchase_order_item_id",
                table: "purchase_stock_in_item",
                column: "purchase_order_item_id");

            migrationBuilder.CreateIndex(
                name: "IX_purchase_stock_in_item_purchase_stock_in_id",
                table: "purchase_stock_in_item",
                column: "purchase_stock_in_id");

            migrationBuilder.CreateIndex(
                name: "IX_workshop_department_id",
                table: "workshop",
                column: "department_id");

            migrationBuilder.CreateIndex(
                name: "IX_workshop_manager_id",
                table: "workshop",
                column: "manager_id");

            migrationBuilder.CreateIndex(
                name: "IX_workshop_workshop_code",
                table: "workshop",
                column: "workshop_code",
                unique: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "production_stock_in_item");

            migrationBuilder.DropTable(
                name: "purchase_stock_in_item");

            migrationBuilder.DropTable(
                name: "production_stock_in");

            migrationBuilder.DropTable(
                name: "purchase_stock_in");

            migrationBuilder.DropTable(
                name: "workshop");
        }
    }
}
